/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlikBankowyDialog.java
 *
 * Created on 2011-05-31, 01:52:30
 */

package homebudget;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import wczytajCSV.AnalizaTabeli;
import wczytajCSV.CSV;
import wczytajCSV.fileFilter;

/**
 *
 * @author lukasz
 */
public class PlikBankowyDialog extends javax.swing.JDialog {

    /** Creates new form PlikBankowyDialog */
    public PlikBankowyDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        //jPanelWybierzBank.setVisible(false);
        //jPanelTabelka.setVisible(false);
        jPanelTabelka.setEnabled(false);
        jPanelWybierzBank.setEnabled(false);
        btnDane.setEnabled(false);
        jButtonDodajDoBazy.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnWczytaj = new javax.swing.JButton();
        lblPlik = new javax.swing.JLabel();
        jPanelWybierzBank = new javax.swing.JPanel();
        btnDane = new javax.swing.JButton();
        jListaBank = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jPanelTabelka = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBoxWydatek = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jComboBoxPrzychod = new javax.swing.JComboBox();
        jButtonDodajDoBazy = new javax.swing.JButton();
        jLabelBlad = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(homebudget.HomeBudgetApp.class).getContext().getResourceMap(PlikBankowyDialog.class);
        btnWczytaj.setText(resourceMap.getString("btnWczytaj.text")); // NOI18N
        btnWczytaj.setName("btnWczytaj"); // NOI18N
        btnWczytaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnWczytajActionPerformed(evt);
            }
        });

        lblPlik.setText(resourceMap.getString("lblPlik.text")); // NOI18N
        lblPlik.setName("lblPlik"); // NOI18N

        jPanelWybierzBank.setName("jPanelWybierzBank"); // NOI18N

        btnDane.setText(resourceMap.getString("btnDane.text")); // NOI18N
        btnDane.setName("btnDane"); // NOI18N
        btnDane.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDaneActionPerformed(evt);
            }
        });

        jListaBank.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "iPKO", "Kredyt Bank"}));
        jListaBank.setName("jListaBank"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout jPanelWybierzBankLayout = new javax.swing.GroupLayout(jPanelWybierzBank);
        jPanelWybierzBank.setLayout(jPanelWybierzBankLayout);
        jPanelWybierzBankLayout.setHorizontalGroup(
            jPanelWybierzBankLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelWybierzBankLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(8, 8, 8)
                .addComponent(jListaBank, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDane)
                .addContainerGap(362, Short.MAX_VALUE))
        );
        jPanelWybierzBankLayout.setVerticalGroup(
            jPanelWybierzBankLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelWybierzBankLayout.createSequentialGroup()
                .addGroup(jPanelWybierzBankLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jListaBank, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDane)
                    .addComponent(jLabel1))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jPanelTabelka.setName("jPanelTabelka"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jComboBoxWydatek.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "" }));
        jComboBoxWydatek.setName("jComboBoxWydatek"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jComboBoxPrzychod.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "" }));
        jComboBoxPrzychod.setName("jComboBoxPrzychod"); // NOI18N

        jButtonDodajDoBazy.setText(resourceMap.getString("jButtonDodajDoBazy.text")); // NOI18N
        jButtonDodajDoBazy.setName("jButtonDodajDoBazy"); // NOI18N
        jButtonDodajDoBazy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDodajDoBazyActionPerformed(evt);
            }
        });

        jLabelBlad.setForeground(resourceMap.getColor("jLabelBlad.foreground")); // NOI18N
        jLabelBlad.setText(resourceMap.getString("jLabelBlad.text")); // NOI18N
        jLabelBlad.setName("jLabelBlad"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxWydatek, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxPrzychod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonDodajDoBazy))
                    .addComponent(jLabelBlad))
                .addContainerGap(185, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBoxWydatek, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBoxPrzychod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonDodajDoBazy))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabelBlad))
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTable1MouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanelTabelkaLayout = new javax.swing.GroupLayout(jPanelTabelka);
        jPanelTabelka.setLayout(jPanelTabelkaLayout);
        jPanelTabelkaLayout.setHorizontalGroup(
            jPanelTabelkaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTabelkaLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(20, 20, 20))
            .addGroup(jPanelTabelkaLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelTabelkaLayout.setVerticalGroup(
            jPanelTabelkaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTabelkaLayout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelTabelka, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(btnWczytaj)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblPlik, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE))
                            .addComponent(jPanelWybierzBank, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(21, 21, 21)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPlik, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(btnWczytaj))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelWybierzBank, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelTabelka, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnWczytajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnWczytajActionPerformed
        // TODO add your handling code here:
        final JFileChooser fc = new JFileChooser();
        fc.setFileFilter(new fileFilter());
        int returnVal = fc.showOpenDialog(jPanelWybierzBank);
        if (returnVal == JFileChooser.APPROVE_OPTION ){
            File file = fc.getSelectedFile();
            lblPlik.setText(file.getAbsolutePath());
            jPanelWybierzBank.setEnabled(true);
            btnDane.setEnabled(true);
        }
    }//GEN-LAST:event_btnWczytajActionPerformed

    private void btnDaneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDaneActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            jPanelTabelka.setVisible(true);
            String filename = lblPlik.getText();
            String nazwaBanku = (String) jListaBank.getSelectedItem();

            CSV model = new CSV(nazwaBanku, filename);
            jTable1 = model.autoResizeColWidth(jTable1, model);


            DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
            LinkedList<String> wydatek = baza.listaKategoriaWydatek();
            LinkedList<String> przychod = baza.listaKategoriaPrzychod();

            ComboBoxModel modelKategoii = new DefaultComboBoxModel(wydatek.toArray());
            jComboBoxWydatek.setModel(modelKategoii);
            modelKategoii = new DefaultComboBoxModel(przychod.toArray());
            jComboBoxPrzychod.setModel(modelKategoii);


            jButtonDodajDoBazy.setEnabled(true);
            baza.close();

        } catch (IOException ex) {
            Logger.getLogger(PlikBankowyDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnDaneActionPerformed

    private void jButtonDodajDoBazyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDodajDoBazyActionPerformed
        // TODO add your handling code here:
         DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
        int idKategorii;
        String nazwaKategorii;
        int[] tablicaZaznaczonych = jTable1.getSelectedRows();
        CSV model = (CSV) jTable1.getModel();
        if(jComboBoxWydatek.isEnabled()){
            nazwaKategorii = (String)jComboBoxWydatek.getSelectedItem();
            idKategorii = baza.idKatWydatek(nazwaKategorii);

            String dataOperacji="";
            String typOperacji="";
            String opisOperacji="";
            String kwota="";


            for(int i=0;i<tablicaZaznaczonych.length;i++){
                dataOperacji = (String)model.getValueAt(tablicaZaznaczonych[i], 0);
                typOperacji = (String)model.getValueAt(tablicaZaznaczonych[i], 1);
                opisOperacji = (String)model.getValueAt(tablicaZaznaczonych[i], 3);
                kwota = (String)model.getValueAt(tablicaZaznaczonych[i], 2);

                int idWydatek = baza.dodajWydatek(typOperacji, idKategorii, Double.parseDouble(kwota)*-1,dataOperacji);
                baza.dodajBudzetWydatek(HomeBudgetApp.getIdbudzetu(), idWydatek);
                baza.dodajProduktDoWydatku(idWydatek, 1, 0, 0);
            }


        }else
            if(jComboBoxPrzychod.isEnabled()){
                nazwaKategorii = (String)jComboBoxPrzychod.getSelectedItem();
                idKategorii = baza.idKatPrzychod(nazwaKategorii);

                String dataOperacji="";
                String typOperacji="";
                String opisOperacji="";
                String kwota="";


                for(int i=0;i<tablicaZaznaczonych.length;i++){
                    dataOperacji = (String)model.getValueAt(tablicaZaznaczonych[i], 0);
                    typOperacji = (String)model.getValueAt(tablicaZaznaczonych[i], 1);
                    opisOperacji = (String)model.getValueAt(tablicaZaznaczonych[i], 3);
                    kwota = (String)model.getValueAt(tablicaZaznaczonych[i], 2);
                    int idPrzychod = baza.dodajPrzychod(typOperacji, idKategorii, Double.parseDouble(kwota),dataOperacji);
                    baza.dodajBudzetPrzychod(HomeBudgetApp.getIdbudzetu(), idPrzychod);
                }
            }
       //usuwanie wierszy zaznaczonych
       for(int i : tablicaZaznaczonych )
            model.removeRow(i);
       jTable1 = model.autoResizeColWidth(jTable1, model);
    }//GEN-LAST:event_jButtonDodajDoBazyActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int wynik = AnalizaTabeli.czyRozneKategorie(jTable1);

      if(wynik==-1){
          jComboBoxWydatek.setEnabled(true);
          jComboBoxPrzychod.setEnabled(false);
          jLabelBlad.setText("");
      }else
          if(wynik==1){
              jComboBoxWydatek.setEnabled(false);
              jComboBoxPrzychod.setEnabled(true);
              jLabelBlad.setText("");
          }else{
          jComboBoxWydatek.setEnabled(false);
          jComboBoxPrzychod.setEnabled(false);
          jLabelBlad.setText("Zaznaczyłes pozycje pasujące do obu kategorii.");
      }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jTable1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseReleased
        // TODO add your handling code here:
        int wynik = AnalizaTabeli.czyRozneKategorie(jTable1);

      if(wynik==-1){
          jComboBoxWydatek.setEnabled(true);
          jComboBoxPrzychod.setEnabled(false);
          jLabelBlad.setText("");
      }else
          if(wynik==1){
              jComboBoxWydatek.setEnabled(false);
              jComboBoxPrzychod.setEnabled(true);
              jLabelBlad.setText("");
          }else{
          jComboBoxWydatek.setEnabled(false);
          jComboBoxPrzychod.setEnabled(false);
          jLabelBlad.setText("Zaznaczyłes pozycje pasujące do obu kategorii.");
      }
    }//GEN-LAST:event_jTable1MouseReleased

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PlikBankowyDialog dialog = new PlikBankowyDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDane;
    private javax.swing.JButton btnWczytaj;
    private javax.swing.JButton jButtonDodajDoBazy;
    private javax.swing.JComboBox jComboBoxPrzychod;
    private javax.swing.JComboBox jComboBoxWydatek;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelBlad;
    private javax.swing.JComboBox jListaBank;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelTabelka;
    private javax.swing.JPanel jPanelWybierzBank;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblPlik;
    // End of variables declaration//GEN-END:variables

}
